<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Services\Impl;

use App\Common\Constant\CacheKey;
use App\Common\Constant\CookieKey;
use App\Models\Master;
use App\Services\ICacheService;
use App\Services\ICookieService;
use App\Services\ISessionService;
use App\Common\Constant\SessionKey;
use Cache;
use Cookie;
use Illuminate\Support\Str;
use Log;
use Session;

/**
 * Description of SessionServiceImpl
 *
 * @author ownfi
 */
class SessionServiceImpl implements ISessionService{

    private $cacheService;
    private $cookieService;

    public function __construct(ICacheService $cacheService,ICookieService $cookieService)
    {
        $this->cacheService = $cacheService;
        $this->cookieService = $cookieService;
    }
    
    public function flash($key, $value = null){
        if ($value === null){
            $value = session($key);
            session()->forget($key);
            return $value;
        }else{
            session()->put($key, $value);
        }
    }

    public function genFlashToken()
    {
        $sessionKey = SessionKey::flashTokenKey();
        $token = Str::random(32);
        session()->flash($sessionKey, $token);
        return session()->has($sessionKey) ? $token : false;
    }

    public function getFlashToken()
    {
        return session(SessionKey::flashTokenKey());
    }

    public function getRedirectUrl($default)
    {
        return $this->flash(SessionKey::redirectUrlKey()) ? : url($default);
    }

    public function setRedirectUrl($redirectUrl)
    {
        return $this->flash(SessionKey::redirectUrlKey(),$redirectUrl);
    }

    public function getRefererUrl($default = null)
    {
        return session(SessionKey::refererUrlKey()) ? : ($default ? url($default) : request()->header('referer'));
    }

    public function setRefererUrl($refererUrl)
    {
        $sessionKey = SessionKey::refererUrlKey();
        session([$sessionKey=>$refererUrl]);
        return session()->has($sessionKey);
    }

    /**
     * 获取当前用户
     */
    public function getCurrentUser() {
        if (isMock()){
            if ($master = Master::where('real_name','黄伟东')->first()){
                return $master;
            }
//            if ($master = Master::find(6182)){
//                return $master;
//            }
        }
        return $this->cacheService->currentUser(getCurrentWeixinUserid());
//        return session(SessionKey::currentUserKey());
    }
    
    /**
     * 设置当前用户
     */
    public function setCurrentUser(Master $user) {
        if (empty($user)){
            return false;
        }
        return $this->cacheService->currentUser(getCurrentWeixinUserid(),$user);
//        $sessionKey = SessionKey::currentUserKey();
//        session([$sessionKey=>$user]);
//        return session()->has($sessionKey);
    }
    
    /**
     * 获取当前微信企业号成员用户
     */
    public function getCurrentWeixinUser(){
        if (isMock()){
            return (Object)["UserId"=>'WuXiao',"DeviceId"=>"DEVICEID_99999999"];
        }
        return session(SessionKey::currentWeixinUserKey());
    }
    
    /**
     * 设置当前微信企业号成员用户
     */
    public function setCurrentWeixinUser($weixinUser){
        if (empty($weixinUser)){
            return false;
        }
        $sessionKey = SessionKey::currentWeixinUserKey();
        session([$sessionKey=>$weixinUser]);
        return session()->has($sessionKey);
    }
    
    public function getCurrentWeixinUserinfo(){
        if (isMock()){
            return (Object)["usertype"=>5,"user_info"=>['userid'=>'WuXiao','name'=>'WuXiao','avatar'=>'xxxx']];
        }
        return session(SessionKey::currentWeixinUserinfoKey());
    }
    
    public function setCurrentWeixinUserinfo($weixinUserinfo){
        if (empty($weixinUserinfo)){
            return false;
        }
        $sessionKey = SessionKey::currentWeixinUserinfoKey();
        session([$sessionKey=>$weixinUserinfo]);
        return session()->has($sessionKey);
    }

    public function logout(){
        $currentWeixinUseridKey = CookieKey::currentWeixinUseridKey();
        Cookie::queue(Cookie::forget(CookieKey::currentUseridKey()));
        Cookie::queue(Cookie::forget($currentWeixinUseridKey));
        Cache::forget(CacheKey::currentUserKey(request()->cookie($currentWeixinUseridKey)));
        Session::forget(SessionKey::currentUserKey());
        Session::forget(SessionKey::currentWeixinUserKey());
    }
}
